/** address.js */
var validate = require('../../utils/validate.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    provinces:[],
    provinceIndex:0,
    cities:[],
    cityIndex:0,
    districts:[],
    districtIndex:0,
    address:{},
    buttonName:'确认新增'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var paramName = "address.addressId";
    this.setData({
      [paramName]: typeof(options.addressId) == "undefined"?'':options.addressId
    }) 
    this.getData();
  },
  // 页面渲染完成
  onReady:function(){

  },
  getData: function () {
    if(this.data.address.addressId == "")
      this.allProvinces(false);
    else if(this.data.address.addressId != ""){//修改地址
      this.setData({
        buttonName: "确认修改"
      });
      this.queryAddress(this.data.address.addressId);
      this.allProvinces(true);
    }
  },
  queryAddress:function(addressId){
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'address/queryAddress.html',
      data: {addressId:addressId},
      header: getApp().globalData.header,
      success: function (res) {
        that.setData({
          address: res.data
        });
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  allProvinces:function(modify){
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'area/allProvinces.html',
      header: getApp().globalData.header,
      success: function (res) {
        //如果是新增，则默认省份为[云南省]
        if(!modify){
          for(var i = 0;i < res.data.length;i++){
            if(res.data[i].provinceName.indexOf('云南')>=0){
              that.data.provinceIndex = i;
              break;
            }
          }
        }else{
          for(var i = 0;i < res.data.length;i++){
            if(res.data[i].provinceId==that.data.address.province){
              that.data.provinceIndex = i;
              break;
            }
          }
        }
        that.setData({
          provinces: res.data,
          provinceIndex:that.data.provinceIndex
        });
        that.queryCities(that.data.provinces[that.data.provinceIndex].provinceId,modify);
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  queryCities:function(provinceId,modify){
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'area/queryCities.html',
      data:{provinceId:provinceId},
      header: getApp().globalData.header,
      success: function (res) {
        if(modify){
          for(var i = 0;i < res.data.length;i++){
            if(res.data[i].cityId==that.data.address.city){
              that.data.cityIndex = i;
              break;
            }
          }
        }
        that.setData({
          cities: res.data,
          cityIndex:that.data.cityIndex
        });
        that.queryDistricts(that.data.cities[that.data.cityIndex].cityId,modify);
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  queryDistricts:function(cityId,modify){
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'area/queryDistricts.html',
      data:{cityId:cityId},
      header: getApp().globalData.header,
      success: function (res) {
        if(modify){
          for(var i = 0;i < res.data.length;i++){
            if(res.data[i].districtId==that.data.address.area){
              that.data.districtIndex = i;
              break;
            }
          }
        }
        that.setData({
          districts: res.data,
          districtIndex: that.data.districtIndex
        });
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
  bindProvinceChange: function(e) {
    this.setData({
      provinceIndex: e.detail.value
    })
    this.queryCities(this.data.provinces[this.data.provinceIndex].provinceId,false);
  },
  bindCityChange: function(e) {
    this.setData({
      cityIndex: e.detail.value
    })
    this.queryDistricts(this.data.cities[this.data.cityIndex].cityId,false);
  },
  bindDistrictChange: function(e) {
    this.setData({
      districtIndex: e.detail.value
    })
  },
  bindAddress:function(e){
    var paramName = "address.address";
    this.setData({
      [paramName]: e.detail.value
    })
  },
  bindName:function(e){
    var paramName = "address.userName";
    this.setData({
       [paramName]: e.detail.value
    })
  },
  bindMobile:function(e){
    var paramName = "address.mobile";
    this.setData({
       [paramName]: e.detail.value
    })
  },
  doAction:function(){
    var province = this.data.provinces[this.data.provinceIndex].provinceId;
    var city = this.data.cities[this.data.cityIndex].cityId;
    var area = '';
    var address = this.data.address.address;
    var userName = this.data.address.userName;
    var mobile = this.data.address.mobile;

    if (province.length == 0) {
      wx.showToast({
        title: '请选择省份',
        duration: 2000
      })
      return;
    }

    if (city.length == 0) {
      wx.showToast({
        title: '请选择城市',
        duration: 2000
      })
      return;
    }
    if(this.data.districts.length>0){
      area = this.data.districts[this.data.districtIndex].districtId;
      if (area.length == 0) {
        wx.showToast({
          title: '请选择地区',
          duration: 2000
        })
        return;
      }
    }
    if (typeof(address)=='undefined'||address.length == 0) {
      wx.showToast({
        title: '请输入详细地址',
        duration: 2000
      })
      return;
    }

    if (typeof(userName)=='undefined'||userName.length == 0) {
      wx.showToast({
        title: '请请输入收货人姓名',
        duration: 2000
      })
      return;
    }
    if (typeof(mobile)=='undefined'){
      wx.showToast({
        title: '请请输入收货人手机号码',
        duration: 2000
      })
      return;
    }

    //校验手机号
    if (!validate.validateMobile(mobile))
      return;
    var that = this;
    if(this.data.address.addressId != ""){//修改
      wx.request({
        url: getApp().globalData.host + 'address/modifyDialog.html',
        data: { addressId:this.data.address.addressId,province: province, city: city, area: area, address:address, userName:userName, mobile:mobile },
        header: getApp().globalData.header,
        method:'POST',
        success: function (res) {
          if(res.data.errorCode==0){
            wx.showToast({
              title: "修改成功",
              duration: 2000
            })
            //返回上一页
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];  //上一个页面

            //调用上一个页面的addresses()方法
            prevPage.addresses();
            wx.navigateBack();
          }else{
            wx.showToast({
              title: "修改失败",
              duration: 2000
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { }
      })
    }else{
      wx.request({
        url: getApp().globalData.host + 'address/addDialog.html',
        data: { province: province, city: city, area: area, address:address, userName:userName, mobile:mobile },
        header: getApp().globalData.header,
        method:'POST',
        success: function (res) {
          if(res.data.errorCode==0){
            wx.showToast({
              title: "添加成功",
              duration: 2000
            })
              //返回上一页
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];  //上一个页面

            //调用上一个页面的addresses()方法
            prevPage.addresses();
            wx.navigateBack();
          }else{
            wx.showToast({
              title: "添加失败",
              duration: 2000
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { }
      })
    }
  }
})